package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.dto.RoleDTO;
import com.ruoyi.system.constants.DyRoleKeyConstants;
import com.ruoyi.system.service.IDyCommonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class DyCommonServiceImpl implements IDyCommonService {

    @Override
    public RoleDTO getFirstRole(List<RoleDTO> roles) {
        Map<String,RoleDTO> roleMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(roles)){
            roleMap = roles.stream().collect(Collectors.toMap(RoleDTO::getRoleKey, entity -> entity, (k1, k2) -> k1));
        }
        if (ObjectUtil.isNotEmpty(roleMap.get(DyRoleKeyConstants.roleKeyAdmin))){
            return roleMap.get(DyRoleKeyConstants.roleKeyAdmin);
        }
        if (ObjectUtil.isNotEmpty(roleMap.get(DyRoleKeyConstants.roleKeyCompanyAdmin))){
            return roleMap.get(DyRoleKeyConstants.roleKeyCompanyAdmin);
        }
        if (ObjectUtil.isNotEmpty(roleMap.get(DyRoleKeyConstants.roleKeySubCompany))){
            return roleMap.get(DyRoleKeyConstants.roleKeySubCompany);
        }
        if (ObjectUtil.isNotEmpty(roleMap.get(DyRoleKeyConstants.roleKeyCustomerService))){
            return roleMap.get(DyRoleKeyConstants.roleKeyCustomerService);
        }
        if (ObjectUtil.isNotEmpty(roleMap.get(DyRoleKeyConstants.roleKeyYunYing))){
            return roleMap.get(DyRoleKeyConstants.roleKeyYunYing);
        }
        return null;
    }

    @Override
    public RoleDTO getRoleByKey(List<RoleDTO> roles,String roleKey) {
        Map<String,RoleDTO> roleMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(roles)){
            roleMap = roles.stream().collect(Collectors.toMap(RoleDTO::getRoleKey, entity -> entity, (k1, k2) -> k1));
        }
        if (ObjectUtil.isNotEmpty(roleMap.get(roleKey))){
            return roleMap.get(roleKey);
        }
        return null;
    }
}
